<?php
class Rule extends Baogg_Db_Table {
	
	protected $_name;
	protected $_db;
	protected $_primary;
	
	function __construct() {
		$this->_name = "rule";
		
		$this->primary = $this->_primary = "rule_id";
		parent::__construct ();
	}
	
	public function qaCompile($str='',$where=array()) {
		$ret=array('compile'=>'','cols'=>array());
		
		if(!$str){
			return array('compile'=>$str,'cols'=>array());;
		}
		
		$QaField=new QaField();
		
		
		preg_match_all('/#([^#]+)#/', $str, $matches);
		
		$col_map=array();//store #price#=>price=>360X1
		$arr_col=array();//store col id
		
		foreach((array)$matches[0] as $k=>$v){
			$col_map[$v]=$matches[1][$k];			
		}
		foreach($col_map as $k=>$v){
			$where=array_merge($where,array('qa_field_name'=>$v));
			$rs_field=$QaField->getList($where);
			if(!$rs_field){
				$ret['compile']='Error!';
				break; 
				//continue;
			}
			$col_map[$k]=$rs_field[0]['qa_field_code'];
			$ret['cols'][$rs_field[0]['qa_field_id']]=$rs_field[0]['qa_field_id'];
		}
		
		$ret['cols']=array_values($ret['cols']);//just for unique
		if(!$ret['compile']){
			$ret['compile']=str_replace(array_keys($col_map),array_values($col_map),$str);
		}
		//echo '<pre>';print_r($ret);print_r($col_map);exit;
		return $ret;
		
	}
}